package com.swt.gap.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.swt.gap.common.constant.CommonConstant;
import com.swt.gap.common.graph.GraphNeo4jVO;
import com.swt.gap.config.ServerConfig;
import com.swt.gap.dao.OntologyNodeDao;
import com.swt.gap.dao.SysIconMapper;
import com.swt.gap.model.entity.OntologyNode;
import com.swt.gap.model.entity.SysIcon;
import com.swt.gap.model.vo.element.*;
import com.swt.gap.model.vo.FormDataVO;
import com.swt.gap.model.vo.ParseEntityVO;
import com.swt.gap.service.CorpusService;

import com.swt.gap.service.EventElementService;
import com.swt.gap.service.GraphDataService;
import com.swt.gap.util.UserLoginUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class CorpusServiceImpl implements CorpusService {

    @Value("${extract.event}")
    private String extractUrl;

    public static final String PROPERTY_NAME = "名称";
    public static final String OTHER_TIME = "TimeArg";
    public static final String DEFAULT_EVENT_TYPE = "自定义事件类型";
    public static final String DEFAULT_EVENT_NAME = "自定义事件";
    public static final String PROPERTY_DESCRIBE = "描述";
    public static final String TIME_BEGIN = "开始时间";
    public static final String TIME_END = "结束时间";
    public static final String PLACE = "地点";
    public static final String STATUS_FAILURE = "failure";

    private final RestTemplate restTemplate;
    private final ServerConfig serverConfig;
    private final GraphDataService graphDataService;
    private final OntologyNodeDao ontologyNodeDao;
    private final EventElementService eventElementService;
    private final SysIconMapper sysIconMapper;

    public CorpusServiceImpl(RestTemplate restTemplate, ServerConfig serverConfig, GraphDataService graphDataService, OntologyNodeDao ontologyNodeDao, EventElementService eventElementService, SysIconMapper sysIconMapper) {
        this.restTemplate = restTemplate;
        this.serverConfig = serverConfig;
        this.graphDataService = graphDataService;
        this.ontologyNodeDao = ontologyNodeDao;
        this.eventElementService = eventElementService;
        this.sysIconMapper = sysIconMapper;
    }

    @Override
    public String readJson(String savedFileName) {

        String filePath = CommonConstant.EXTRACT_DIR + savedFileName;
        String jsonStr = "";
        try {
            File jsonFile = new File(filePath);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ParseEntityVO parseEntity(String savedFileName) {
        ParseEntityVO parseEntityVO = new ParseEntityVO();
        String fileName = savedFileName.substring(savedFileName.lastIndexOf("_") + 1);
        savedFileName = fileName.replace("txt", "json");
        String jsonStr = this.readJson(savedFileName);
        parseEntityVO.setJsonstr(jsonStr);

        //生成表格
        List<String> header = Arrays.asList("事件触发词,地点,参与国家,参与军队,装备,时间,演习内容".split(","));
        List<List<String>> rows = new ArrayList<>();

        //建图数据
        List<GraphNeo4jVO.Node> nodes = new ArrayList<>();
        List<GraphNeo4jVO.Link> links = new ArrayList<>();
        //文本节点
        GraphNeo4jVO.Node fileNode = new GraphNeo4jVO.Node();
        fileNode.setName(fileName);
        fileNode.setId(UUID.randomUUID().toString());
        fileNode.setIcon(serverConfig.parseViewUrl("hg-task-file.png", 3));
        nodes.add(fileNode);

        GraphNeo4jVO.Node entityLabelNode = new GraphNeo4jVO.Node();
        entityLabelNode.setName("事件触发词");
        entityLabelNode.setId(UUID.randomUUID().toString());
        entityLabelNode.setIcon(serverConfig.parseViewUrl("hg-task-exerciseName.png", 3));
        nodes.add(entityLabelNode);

        GraphNeo4jVO.Link link = new GraphNeo4jVO.Link();
        link.setId(UUID.randomUUID().toString());
        link.setStartNodeId(fileNode.getId());
        link.setEndNodeId(entityLabelNode.getId());
        links.add(link);

        GraphNeo4jVO.Node entityLabelNode2 = new GraphNeo4jVO.Node();
        entityLabelNode2.setName("地点");
        entityLabelNode2.setId(UUID.randomUUID().toString());
        entityLabelNode2.setIcon(serverConfig.parseViewUrl("hg-task-place.png", 3));
        nodes.add(entityLabelNode2);

        link = new GraphNeo4jVO.Link();
        link.setId(UUID.randomUUID().toString());
        link.setStartNodeId(fileNode.getId());
        link.setEndNodeId(entityLabelNode2.getId());
        links.add(link);

        GraphNeo4jVO.Node entityLabelNode3 = new GraphNeo4jVO.Node();
        entityLabelNode3.setName("参与国家");
        entityLabelNode3.setId(UUID.randomUUID().toString());
        entityLabelNode3.setIcon(serverConfig.parseViewUrl("hg-task-countries.png", 3));
        nodes.add(entityLabelNode3);

        link = new GraphNeo4jVO.Link();
        link.setId(UUID.randomUUID().toString());
        link.setStartNodeId(fileNode.getId());
        link.setEndNodeId(entityLabelNode3.getId());
        links.add(link);

        GraphNeo4jVO.Node entityLabelNode4 = new GraphNeo4jVO.Node();
        entityLabelNode4.setName("参与军队");
        entityLabelNode4.setId(UUID.randomUUID().toString());
        entityLabelNode4.setIcon(serverConfig.parseViewUrl("hg-task-army.png", 3));
        nodes.add(entityLabelNode4);

        link = new GraphNeo4jVO.Link();
        link.setId(UUID.randomUUID().toString());
        link.setStartNodeId(fileNode.getId());
        link.setEndNodeId(entityLabelNode4.getId());
        links.add(link);

        GraphNeo4jVO.Node entityLabelNode5 = new GraphNeo4jVO.Node();
        entityLabelNode5.setName("装备");
        entityLabelNode5.setId(UUID.randomUUID().toString());
        entityLabelNode5.setIcon(serverConfig.parseViewUrl("hg-task-weapon.png", 3));
        nodes.add(entityLabelNode5);

        link = new GraphNeo4jVO.Link();
        link.setId(UUID.randomUUID().toString());
        link.setStartNodeId(fileNode.getId());
        link.setEndNodeId(entityLabelNode5.getId());
        links.add(link);

        GraphNeo4jVO.Node entityLabelNode6 = new GraphNeo4jVO.Node();
        entityLabelNode6.setName("时间");
        entityLabelNode6.setId(UUID.randomUUID().toString());
        entityLabelNode6.setIcon(serverConfig.parseViewUrl("hg-task-time.png", 3));
        nodes.add(entityLabelNode6);

        link = new GraphNeo4jVO.Link();
        link.setId(UUID.randomUUID().toString());
        link.setStartNodeId(fileNode.getId());
        link.setEndNodeId(entityLabelNode6.getId());
        links.add(link);

        GraphNeo4jVO.Node entityLabelNode7 = new GraphNeo4jVO.Node();
        entityLabelNode7.setName("演习内容");
        entityLabelNode7.setId(UUID.randomUUID().toString());
        entityLabelNode7.setIcon(serverConfig.parseViewUrl("hg-task-content.png", 3));
        nodes.add(entityLabelNode7);

        link = new GraphNeo4jVO.Link();
        link.setId(UUID.randomUUID().toString());
        link.setStartNodeId(fileNode.getId());
        link.setEndNodeId(entityLabelNode7.getId());
        links.add(link);

        JSONObject jobj = JSON.parseObject(jsonStr);
        JSONArray pars = jobj.getJSONArray("pars");

        for (int i = 0 ; i < pars.size();i++){
            JSONObject sents = (JSONObject)pars.get(i);
            JSONArray sentsJSONArray = sents.getJSONArray("sents");

            for (int j = 0; j < sentsJSONArray.size() ; j++) {
                JSONObject txt = (JSONObject) sentsJSONArray.get(j);
                JSONArray pred_event_mentions = txt.getJSONArray("pred_event_mentions");
                if (pred_event_mentions != null && pred_event_mentions.size() != 0) {
                    //定义行内容的数组
                    String[][] rowArray = new String[pred_event_mentions.size()][7];
                    //获取提取事件的数组
                    for (int k = 0; k < pred_event_mentions.size(); k++) {
                        JSONObject pred_event_mention = (JSONObject) pred_event_mentions.get(k);
                        String subtype = pred_event_mention.getString("subtype");
                        if (subtype != null) {
                            //触发词
                            String trigger = pred_event_mention.getString("text");
                            rowArray[k][0] = trigger;

                            GraphNeo4jVO.Node entityNode1 = new GraphNeo4jVO.Node();
                            entityNode1.setName(trigger);
                            entityNode1.setId(UUID.randomUUID().toString());
                            entityNode1.setIcon(serverConfig.parseViewUrl("hg-task-exerciseName.png", 3));
                            nodes.add(entityNode1);

                            link = new GraphNeo4jVO.Link();
                            link.setId(UUID.randomUUID().toString());
                            link.setStartNodeId(entityLabelNode.getId());
                            link.setEndNodeId(entityNode1.getId());
                            links.add(link);
                        }

                        JSONArray argument_Mentions = pred_event_mention.getJSONArray("argument_mentions");
                        //获取一个事件的论元
                        for (int l = 0; l < argument_Mentions.size(); l++) {
                            //一个论元
                            JSONObject argument = (JSONObject) argument_Mentions.get(l);
                            String role = argument.getString("role");
                            String text = argument.getString("text");
                            if ("PlaceArg".equals(role)) {
                                if (rowArray[k][1] == null || rowArray[k][1].equals("")) {
                                    rowArray[k][1] = text;
                                } else {
                                    rowArray[k][1] += "," + text;
                                }

                                GraphNeo4jVO.Node entityNode2 = new GraphNeo4jVO.Node();
                                entityNode2.setName(text);
                                entityNode2.setId(UUID.randomUUID().toString());
                                entityNode2.setIcon(serverConfig.parseViewUrl("hg-task-place.png", 3));
                                nodes.add(entityNode2);

                                link = new GraphNeo4jVO.Link();
                                link.setId(UUID.randomUUID().toString());
                                link.setStartNodeId(entityLabelNode2.getId());
                                link.setEndNodeId(entityNode2.getId());
                                links.add(link);

                            } else if ("ParticipatingCountryArg".equals(role)) {
                                if (rowArray[k][2] == null || rowArray[k][2].equals("")) {
                                    rowArray[k][2] = text;
                                } else {
                                    rowArray[k][2] += "," + text;
                                }
                                GraphNeo4jVO.Node entityNode3 = new GraphNeo4jVO.Node();
                                entityNode3.setName(text);
                                entityNode3.setId(UUID.randomUUID().toString());
                                entityNode3.setIcon(serverConfig.parseViewUrl("hg-task-countries.png", 3));
                                nodes.add(entityNode3);

                                link = new GraphNeo4jVO.Link();
                                link.setId(UUID.randomUUID().toString());
                                link.setStartNodeId(entityLabelNode3.getId());
                                link.setEndNodeId(entityNode3.getId());
                                links.add(link);

                            } else if ("ParticipatingArmyArg".equals(role)) {
                                if (rowArray[k][3] == null || rowArray[k][3].equals("")) {
                                    rowArray[k][3] = text;
                                } else {
                                    rowArray[k][3] += "," + text;
                                }

                                GraphNeo4jVO.Node entityNode4 = new GraphNeo4jVO.Node();
                                entityNode4.setName(text);
                                entityNode4.setId(UUID.randomUUID().toString());
                                entityNode4.setIcon(serverConfig.parseViewUrl("hg-task-army.png", 3));
                                nodes.add(entityNode4);

                                link = new GraphNeo4jVO.Link();
                                link.setId(UUID.randomUUID().toString());
                                link.setStartNodeId(entityLabelNode4.getId());
                                link.setEndNodeId(entityNode4.getId());
                                links.add(link);
                            } else if ("WeaponArg".equals(role)) {
                                if (rowArray[k][4] == null || "".equals(rowArray[k][4])) {
                                    rowArray[k][4] = text;
                                } else {
                                    rowArray[k][4] += "," + text;
                                }

                                GraphNeo4jVO.Node entityNode5 = new GraphNeo4jVO.Node();
                                entityNode5.setName(text);
                                entityNode5.setId(UUID.randomUUID().toString());
                                entityNode5.setIcon(serverConfig.parseViewUrl("hg-task-weapon.png", 3));
                                nodes.add(entityNode5);

                                link = new GraphNeo4jVO.Link();
                                link.setId(UUID.randomUUID().toString());
                                link.setStartNodeId(entityLabelNode5.getId());
                                link.setEndNodeId(entityNode5.getId());
                                links.add(link);
                            } else if ("TimeArg".equals(role)) {
                                if (rowArray[k][5] == null || rowArray[k][5].equals("")) {
                                    rowArray[k][5] = text;
                                } else {
                                    rowArray[k][5] += "," + text;
                                }

                                GraphNeo4jVO.Node entityNode6 = new GraphNeo4jVO.Node();
                                entityNode6.setName(text);
                                entityNode6.setId(UUID.randomUUID().toString());
                                entityNode6.setIcon(serverConfig.parseViewUrl("hg-task-time.png", 3));
                                nodes.add(entityNode6);

                                link = new GraphNeo4jVO.Link();
                                link.setId(UUID.randomUUID().toString());
                                link.setStartNodeId(entityLabelNode6.getId());
                                link.setEndNodeId(entityNode6.getId());
                                links.add(link);
                            } else if ("ExerciseContentArg".equals(role)) {
                                if (rowArray[k][6] == null || rowArray[k][6].equals("")) {
                                    rowArray[k][6] = text;
                                } else {
                                    rowArray[k][6] += "," + text;
                                }

                                GraphNeo4jVO.Node entityNode7 = new GraphNeo4jVO.Node();
                                entityNode7.setName(text);
                                entityNode7.setId(UUID.randomUUID().toString());
                                entityNode7.setIcon(serverConfig.parseViewUrl("hg-task-content.png", 3));
                                nodes.add(entityNode7);

                                link = new GraphNeo4jVO.Link();
                                link.setId(UUID.randomUUID().toString());
                                link.setStartNodeId(entityLabelNode7.getId());
                                link.setEndNodeId(entityNode7.getId());
                                links.add(link);
                            }

                        }


                    }
                    for (String[] strings : rowArray) {
                        rows.add(Arrays.asList(strings));
                    }
                }
            }
        }
        parseEntityVO.setFormData(new FormDataVO(header, rows));
        GraphNeo4jVO graph = new GraphNeo4jVO(nodes, links);
        parseEntityVO.setGraph(graph);
        return parseEntityVO;

    }

    @Override
    public GraphNeo4jVO parseGraph(List<ElementParseVO.EventExtractVO> eventExtractVOS, String savedFileName) {
        String fileName = savedFileName.substring(savedFileName.lastIndexOf("_") + 1);
        //建图数据
        List<GraphNeo4jVO.Node> nodes = new ArrayList<>();
        List<GraphNeo4jVO.Link> links = new ArrayList<>();
        //文本节点
        GraphNeo4jVO.Node fileNode = new GraphNeo4jVO.Node();
        fileNode.setName(fileName);
        fileNode.setId(UUID.randomUUID().toString());
        fileNode.setIcon(serverConfig.parseViewUrl("hg-task-file.png", 3));
        nodes.add(fileNode);

        eventExtractVOS.forEach(eventExtractVO -> {
            //事件类型节点
            GraphNeo4jVO.Node entityLabelNode = new GraphNeo4jVO.Node();
            entityLabelNode.setName(eventExtractVO.getEventTypeName());
            entityLabelNode.setId(UUID.randomUUID().toString());
            if (eventExtractVO.getEventTypeId() != null) {
                OntologyNode ontologyNode = ontologyNodeDao.selectByPrimaryKey(eventExtractVO.getEventTypeId());
                SysIcon sysIcon = sysIconMapper.selectByPrimaryKey(ontologyNode.getIconId());
                entityLabelNode.setIcon(serverConfig.parseViewUrl(sysIcon.getIconFile(), 3));
            } else {
                entityLabelNode.setIcon(serverConfig.parseViewUrl("hg-event-element.png", 3));
            }

            nodes.add(entityLabelNode);

            GraphNeo4jVO.Link link = new GraphNeo4jVO.Link();
            link.setId(UUID.randomUUID().toString());
            link.setStartNodeId(fileNode.getId());
            link.setEndNodeId(entityLabelNode.getId());
            links.add(link);

            //要素类型节点
            for (int i = 0; i < eventExtractVO.getElementVOS().size(); i++) {
                EventElementVO eventElementVO = eventExtractVO.getElementVOS().get(i);
                GraphNeo4jVO.Node elementLabelNode = new GraphNeo4jVO.Node();
                elementLabelNode.setName(eventElementVO.getElement());
                elementLabelNode.setId(UUID.randomUUID().toString());
                elementLabelNode.setIcon(serverConfig.parseViewUrl("hg-event-element.png", 3));
                nodes.add(elementLabelNode);

                GraphNeo4jVO.Link elementLink = new GraphNeo4jVO.Link();
                elementLink.setId(UUID.randomUUID().toString());
                elementLink.setStartNodeId(entityLabelNode.getId());
                elementLink.setEndNodeId(elementLabelNode.getId());
                links.add(elementLink);

                //事件节点
                for (int k = 0; k < eventExtractVO.getEventLists().size(); k++) {
                    ElementParseVO.EventExtractVO.EventList eventList = eventExtractVO.getEventLists().get(k);
                    List<EventContentVO> eventContentVOS = eventList.getEventContentVOS();
                    if (!CollectionUtils.isEmpty(eventContentVOS)) {

                        for (int n = 0; n < eventList.getHeader().size(); n++) {
                            String header = eventList.getHeader().get(n);
                            for (EventContentVO eventContentVO : eventContentVOS) {
                                if (eventContentVO.getIndex() == n && header.equals(eventElementVO.getElement())) {
                                    if (!CollectionUtils.isEmpty(eventContentVO.getContentVOS())) {
                                        for (ContentVO contentVO : eventContentVO.getContentVOS()) {
                                            GraphNeo4jVO.Node eventNode = new GraphNeo4jVO.Node();
                                            eventNode.setName(contentVO.getContent());
                                            eventNode.setId(UUID.randomUUID().toString());
                                            eventNode.setIcon(serverConfig.parseViewUrl("hg-event-element.png", 3));
                                            nodes.add(eventNode);

                                            GraphNeo4jVO.Link eventLink = new GraphNeo4jVO.Link();
                                            eventLink.setId(UUID.randomUUID().toString());
                                            eventLink.setStartNodeId(elementLabelNode.getId());
                                            eventLink.setEndNodeId(eventNode.getId());
                                            links.add(eventLink);
                                        }
                                    }
                                }
                            }
                        }


                    }

                }



            }
        });

        GraphNeo4jVO graph = new GraphNeo4jVO(nodes, links);
        return graph;
    }

    @Override
    public ElementParseVO extractCombinedEvents(String savedFileName) throws Exception {
        Integer userId = UserLoginUtils.getUserId();
        ElementParseVO elementParseVO = new ElementParseVO();

//        String url="http://ws.nju.edu.cn/ila/extract";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //定义body里的传参
        HashMap<String, Object> bodyMap = new HashMap<>();
        // id为任意字符串，用来区分不同的请求
        bodyMap.put("id",new Random(1).nextInt(100));
        String textFileData = graphDataService.getTextFileData(savedFileName);
        // 要做信息抽取的文段，可以包含多个段落
        bodyMap.put("text",textFileData);
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(bodyMap, headers);
        CombinedResult combinedResult = restTemplate.postForEntity(extractUrl, entity, CombinedResult.class).getBody();
        elementParseVO.setCombinedResult(combinedResult);


        //获取列表展示结果
        List<ElementParseVO.EventExtractVO> eventExtractVOS = new ArrayList<>();
        //获取所有的事件类型
        ontologyNodeDao.selectByType(2).forEach(ontologyEntity -> {
            ElementParseVO.EventExtractVO eventExtractVO = new ElementParseVO.EventExtractVO();
            eventExtractVO.setEventTypeId(ontologyEntity.getNodeId());
            eventExtractVO.setEventTypeName(StringUtils.substringAfterLast(ontologyEntity.getLabels(), ","));
            eventExtractVO.setElementVOS(eventElementService.AllElements(ontologyEntity.getNodeId(), userId));
            int count = 0;
            eventExtractVO.setCount(count);
            List<ElementParseVO.EventExtractVO.EventList> eventLists = new ArrayList<>();
            eventExtractVO.setEventLists(eventLists);
            eventExtractVOS.add(eventExtractVO);
        });

        ResultsBean results = combinedResult.getResults();
        String status = combinedResult.getStatus();
         //提取失败或者没内容时默认给个表头
        if (null == results || STATUS_FAILURE.equals(status)) {
//            List<ElementParseVO.EventExtractVO> eventExtract = this.createDefaultForm();
//            elementParseVO.setEventExtractVOS(eventExtract);
//            return elementParseVO;
            return null;
        }


        List<ResultsBean.CombinedEventsBean> combinedEvents = results.getCombined_events();
        if (!CollectionUtils.isEmpty(combinedEvents)) {
            for (ResultsBean.CombinedEventsBean combinedEvent : combinedEvents) {
                //获取事件子类型英文简称,事件名，触发词，事件论元
                String eventName = combinedEvent.getName();
                String subtype = combinedEvent.getSubtype();
                String subtypeCn = combinedEvent.getSubtype_cn();
                List<ResultsBean.CombinedEventsBean.CombinedArgumentsBean> combinedArguments = combinedEvent.getCombined_arguments();
                //判断论元和事件名称是否为空
                if (!StringUtils.isEmpty(eventName) || !CollectionUtils.isEmpty(combinedArguments)) {
                    OntologyNode ontologyNode = ontologyNodeDao.selectByEnglishName(subtype);
                    if (ontologyNode != null) {
                        String eventTypeName = StringUtils.substringAfterLast(ontologyNode.getLabels(), ",");
                        //以事件类型区分
                        eventExtractVOS.forEach(eventExtractVO -> {
                            if (eventExtractVO.getEventTypeName().equals(eventTypeName)) {
                                eventExtractVO.setCount(eventExtractVO.getCount() + 1);

                                ElementParseVO.EventExtractVO.EventList eventList = new ElementParseVO.EventExtractVO.EventList();
                                if (!StringUtils.isEmpty(eventName)) {
                                    eventList.setEventName(eventName);
                                } else {
                                    eventList.setEventName(subtypeCn);
                                }
//                                if (!CollectionUtils.isEmpty(combinedArguments)) {
                                    List<String> header = new ArrayList<>();
                                    List<EventElementVO> elementVOS = eventExtractVO.getElementVOS();
                                    elementVOS.forEach(eventElementVO -> header.add(eventElementVO.getElement()));
                                    eventList.setHeader(header);

                                    List<EventContentVO> eventContentVOS = new ArrayList<>();
                                    for (int i = 0; i < header.size(); i++) {
                                        List<ContentVO> contentVOS = new ArrayList<>();
                                        if (header.get(i).equals(PROPERTY_NAME)) {
                                            ContentVO contentVO = new ContentVO();
                                            contentVO.setContent(eventList.getEventName());
                                            contentVOS.add(contentVO);
                                        }
                                        EventContentVO eventContentVO = new EventContentVO();
                                        eventContentVO.setIndex(i);
                                        eventContentVO.setContentVOS(contentVOS);
                                        eventContentVOS.add(eventContentVO);
                                    }

                                    combinedArguments.forEach(argumentMentionsBean -> {
                                        String role = argumentMentionsBean.getRole();
                                        AtomicReference<String> roleCn = new AtomicReference<>("");
                                        elementVOS.forEach(eventElementVO -> {
                                            String englishName = eventElementVO.getEnglishName();
                                            if (!StringUtils.isEmpty(englishName)) {
                                                if (role.equals(englishName)) {
                                                    roleCn.set(eventElementVO.getElement());
                                                }

                                            }
                                        });

                                        for (int i = 0; i < header.size(); i++) {
                                            for (EventContentVO eventContentVO : eventContentVOS) {
                                                if (eventContentVO.getIndex() == i) {
                                                    if (header.get(i).equals(roleCn.get())) {
                                                        ContentVO contentVO = new ContentVO();
                                                        contentVO.setContent(argumentMentionsBean.getNormalization() == null ? argumentMentionsBean.getText() : argumentMentionsBean.getNormalization());
                                                        contentVO.setBegin(argumentMentionsBean.getBegin());
                                                        contentVO.setEnd(argumentMentionsBean.getEnd());
                                                        List<ContentVO> contentVOS = eventContentVO.getContentVOS();
                                                        contentVOS.add(contentVO);
                                                        eventContentVO.setContentVOS(contentVOS);
                                                    }
                                                }
                                            }
                                        }
                                    });

                                    eventList.setEventContentVOS(eventContentVOS);

//                                }
                                List<ElementParseVO.EventExtractVO.EventList> eventLists = eventExtractVO.getEventLists();
                                eventLists.add(eventList);
                                eventExtractVO.setEventLists(eventLists);
                            }
                        });
                    }
                }

            }
        } else {
            List<ElementParseVO.EventExtractVO> eventExtract = this.createDefaultForm();
            elementParseVO.setEventExtractVOS(eventExtract);
            return elementParseVO;
        }


        //剔除掉事件数量为0的事件类型
        List<ElementParseVO.EventExtractVO> extractVOS = new ArrayList<>();
        //供选择的事件类型列表
        List<ElementParseVO.EventExtractVO> otherEventTypes = new ArrayList<>();
        for (ElementParseVO.EventExtractVO eventExtractVO : eventExtractVOS) {
            if (eventExtractVO.getCount() != 0) {
                extractVOS.add(eventExtractVO);
            } else {
                otherEventTypes.add(eventExtractVO);
            }
        }
        elementParseVO.setEventExtractVOS(extractVOS);

        elementParseVO.setOtherEventExtract(otherEventTypes);
        /*String fileName = savedFileName.substring(savedFileName.lastIndexOf("_") + 1);
        //建图数据
        List<GraphNeo4jVO.Node> nodes = new ArrayList<>();
        List<GraphNeo4jVO.Link> links = new ArrayList<>();
        //文本节点
        GraphNeo4jVO.Node fileNode = new GraphNeo4jVO.Node();
        fileNode.setName(fileName);
        fileNode.setId(UUID.randomUUID().toString());
        fileNode.setIcon(serverConfig.parseViewUrl("hg-task-file.png", 3));
        nodes.add(fileNode);

        extractVOS.forEach(eventExtractVO -> {
            //事件类型节点
            GraphNeo4jVO.Node entityLabelNode = new GraphNeo4jVO.Node();
            entityLabelNode.setName(eventExtractVO.getEventTypeName());
            entityLabelNode.setId(UUID.randomUUID().toString());
            OntologyNode ontologyNode = ontologyNodeMapper.selectByPrimaryKey(eventExtractVO.getEventTypeId());
            SysIcon sysIcon = sysIconMapper.selectByPrimaryKey(ontologyNode.getIconId());
            entityLabelNode.setIcon(serverConfig.parseViewUrl(sysIcon.getIconFile(), 3));
            nodes.add(entityLabelNode);

            GraphNeo4jVO.Link link = new GraphNeo4jVO.Link();
            link.setId(UUID.randomUUID().toString());
            link.setStartNodeId(fileNode.getId());
            link.setEndNodeId(entityLabelNode.getId());
            links.add(link);

            //要素类型节点
            for (int i = 0; i < eventExtractVO.getElementVOS().size(); i++) {
                EventElementVO eventElementVO = eventExtractVO.getElementVOS().get(i);
                GraphNeo4jVO.Node elementLabelNode = new GraphNeo4jVO.Node();
                elementLabelNode.setName(eventElementVO.getElement());
                elementLabelNode.setId(UUID.randomUUID().toString());
                elementLabelNode.setIcon(serverConfig.parseViewUrl("hg-event-element.png", 3));
                nodes.add(elementLabelNode);

                GraphNeo4jVO.Link elementLink = new GraphNeo4jVO.Link();
                elementLink.setId(UUID.randomUUID().toString());
                elementLink.setStartNodeId(entityLabelNode.getId());
                elementLink.setEndNodeId(elementLabelNode.getId());
                links.add(elementLink);

                //事件节点
                for (int k = 0; k < eventExtractVO.getEventLists().size(); k++) {
                    ElementParseVO.EventExtractVO.EventList eventList = eventExtractVO.getEventLists().get(k);
                    List<EventContentVO> eventContentVOS = eventList.getEventContentVOS();
                    if (!CollectionUtils.isEmpty(eventContentVOS)) {

                        for (int n = 0; n < eventList.getHeader().size(); n++) {
                            String header = eventList.getHeader().get(n);
                            for (EventContentVO eventContentVO : eventContentVOS) {
                                if (eventContentVO.getIndex() == n && header.equals(eventElementVO.getElement())) {
                                    if (!CollectionUtils.isEmpty(eventContentVO.getContentVOS())) {
                                        for (ContentVO contentVO : eventContentVO.getContentVOS()) {
                                            GraphNeo4jVO.Node eventNode = new GraphNeo4jVO.Node();
                                            eventNode.setName(contentVO.getContent());
                                            eventNode.setId(UUID.randomUUID().toString());
                                            eventNode.setIcon(serverConfig.parseViewUrl("hg-event-element.png", 3));
                                            nodes.add(eventNode);

                                            GraphNeo4jVO.Link eventLink = new GraphNeo4jVO.Link();
                                            eventLink.setId(UUID.randomUUID().toString());
                                            eventLink.setStartNodeId(elementLabelNode.getId());
                                            eventLink.setEndNodeId(eventNode.getId());
                                            links.add(eventLink);
                                        }
                                    }
                                }
                            }
                        }


                    }

                }



            }
        });

        GraphNeo4jVO graph = new GraphNeo4jVO(nodes, links);
        elementParseVO.setGraph(graph);*/
        return elementParseVO;
    }

    @Override
    public List<ElementParseVO.EventExtractVO> createDefaultForm() {
        List<ElementParseVO.EventExtractVO> eventExtract = new ArrayList<>();
        ElementParseVO.EventExtractVO eventExtractVO = new ElementParseVO.EventExtractVO();
        eventExtractVO.setEventTypeName(DEFAULT_EVENT_TYPE);
        eventExtractVO.setCount(1);
        List<EventElementVO> eventElementVOS = new ArrayList<>();
        String[] elements = {PROPERTY_NAME, PROPERTY_DESCRIBE, TIME_BEGIN, TIME_END, PLACE};
        for (String element : elements) {
            EventElementVO eventElementVO = new EventElementVO();
            eventElementVO.setElement(element);
            eventElementVO.setType(1);
            eventElementVOS.add(eventElementVO);
        }
        eventExtractVO.setElementVOS(eventElementVOS);
        List<ElementParseVO.EventExtractVO.EventList> eventLists = new ArrayList<>();
        ElementParseVO.EventExtractVO.EventList eventList = new ElementParseVO.EventExtractVO.EventList();
        eventList.setEventName(DEFAULT_EVENT_NAME);
        eventList.setHeader(Arrays.asList(elements));
        List<EventContentVO> eventContentVOS = new ArrayList<>();
        for (int i = 0; i < eventList.getHeader().size(); i++) {
            List<ContentVO> contentVOS = new ArrayList<>();
            if (eventList.getHeader().get(i).equals(PROPERTY_NAME)) {
                ContentVO contentVO = new ContentVO();
                contentVO.setContent(DEFAULT_EVENT_NAME);
                contentVOS.add(contentVO);
            }
            EventContentVO eventContentVO = new EventContentVO();
            eventContentVO.setIndex(i);
            eventContentVO.setContentVOS(contentVOS);
            eventContentVOS.add(eventContentVO);
        }
        eventList.setEventContentVOS(eventContentVOS);
        eventLists.add(eventList);
        eventExtractVO.setEventLists(eventLists);
        eventExtract.add(eventExtractVO);
        return eventExtract;
    }

}
